﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HelloWPF.RouteEvent
{
    /// <summary>
    /// ToolBarUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class ToolBarUserControl : UserControl
    {
        public ToolBarUserControl()
        {
            InitializeComponent();
        }

        // 依赖属性    DependencyProperty.Register     ValueProperty
        // 路由事件
        /// <summary>
        /// 参数说明：
        /// "Tap"：事件名称
        /// RoutingStrategy.Bubble：路由策略：冒泡、隧道、直接
        /// typeof(RoutedEventHandler)：事件类型
        /// typeof(UserControl1)：所拥有者
        /// 
        /// 问题：如何触发？
        /// </summary>
        public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent("Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToolBarUserControl));

        /// <summary>
        /// 包装成CLR事件
        /// 对应事件的名称
        /// </summary>
        public event RoutedEventHandler Tap
        {
            add { AddHandler(TapEvent, value); }
            remove
            {
                RemoveHandler(TapEvent, value);
            }
        }

        public static readonly RoutedEvent PreviewTapEvent = EventManager.RegisterRoutedEvent("PreviewTap", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(ToolBarUserControl));

        /// <summary>
        /// 包装成CLR事件
        /// 对应事件的名称
        /// </summary>
        public event RoutedEventHandler PreviewTap
        {
            add { AddHandler(TapEvent, value); }
            remove
            {
                RemoveHandler(TapEvent, value);
            }
        }
      

        //这个事件已经属于冒泡事件//再处理隧道事件？
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //如果这里不改成冒泡事件的话，那么外层的工具栏用户控件的事件无法触发
            RoutedEventArgs routedEventArgs = new RoutedEventArgs(ToolBarUserControl.TapEvent);
            this.RaiseEvent(routedEventArgs);
        }

        //这个事件已经属于隧道事件
        private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            RoutedEventArgs routedEventArgs = new RoutedEventArgs(ToolBarUserControl.PreviewTapEvent);
            this.RaiseEvent(routedEventArgs);
        }
    }
}
